﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using Register.DBUtility;
using System.Data.SqlClient;
using System.Data;
using Register.Model;

namespace Register.DAL
{
    /// <summary>
    /// FileName: PatientUserInfo.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangKungLun
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/7/7 10:07:20
    /// </summary>
    public class PatientUserInfoDAL:IPatientUserInfoDAL
    {
        private string tableName = "System_patientUserInfo";

        public int UpdatePatientUserInfo(Model.Patient model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_patientUserInfo set ");
            strSql.Append("PatientUserInfo_UserType=@PatientUserInfo_UserType ");
            strSql.Append(" where PatientUserInfo_ID=@PatientUserInfo_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PatientUserInfo_UserType", SqlDbType.Char,4),
                    new SqlParameter("@PatientUserInfo_ID", SqlDbType.Char,14)};
            parameters[0].Value = model.PatientUserInfo_UserType;
            parameters[1].Value = model.PatientUserInfo_ID;
            return DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        }

        public List<Model.Patient> GetPagedPatientUserInfo(int size, int pageIndex, string where)
        {
            return DataSetToList(DbHelperSQL.getPagedData(" * ", " PatientUserInfo_AddTime ", where, tableName, OrderBy.DESC, pageIndex, size));
        }

        public Model.Patient GetPatientUserInfo(string PatientUserInfo_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PatientUserInfo_ID,PatientUserInfo_Name,PatientUserInfo_Password,PatientUserInfo_Phone,PatientUserInfo_BirthDay,PatientUserInfo_Sex,PatientUserInfo_Mail,PatientUserInfo_IDCrad,PatientUserInfo_UserType,PatientUserInfo_MedicareCard,PatientUserInfo_AddTime,PatientUserInfo_NotComeTime,PatientUserInfo_RrgIP,PatientUserInfo_State from System_patientUserInfo ");

            strSql.Append(" where PatientUserInfo_ID=@PatientUserInfo_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@PatientUserInfo_ID", SqlDbType.Char,14)};
            parameters[0].Value = PatientUserInfo_ID;
            return DataSetToList(DbHelperSQL.Query(strSql.ToString(), parameters)).First();
        }

        private List<Model.Patient> DataSetToList(DataSet ds)
        {
            List<Model.Patient> list = new List<Model.Patient>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Patient patient = new Patient();
                patient.PatientUserInfo_ID = (String)dr["PatientUserInfo_ID"];
                patient.PatientUserInfo_Name = (String)dr["PatientUserInfo_Name"];
                patient.PatientUserInfo_Password = (String)dr["PatientUserInfo_Password"];
                patient.PatientUserInfo_Phone = (String)dr["PatientUserInfo_Phone"];
                patient.PatientUserInfo_BirthDay = (DateTime)dr["PatientUserInfo_BirthDay"];
                patient.PatientUserInfo_Sex = (String)dr["PatientUserInfo_Sex"];
                patient.PatientUserInfo_Mail = (String)dr["PatientUserInfo_Mail"];
                patient.PatientUserInfo_IDCrad = (String)dr["PatientUserInfo_IDCrad"];
                patient.PatientUserInfo_UserType = (String)dr["PatientUserInfo_UserType"];
                patient.PatientUserInfo_MedicareCard = (String)dr["PatientUserInfo_MedicareCard"];
                patient.PatientUserInfo_AddTime = (DateTime)dr["PatientUserInfo_AddTime"];
                patient.PatientUserInfo_NotComeTime = (int)dr["PatientUserInfo_NotComeTime"];
                patient.PatientUserInfo_RrgIP = (String)dr["PatientUserInfo_RrgIP"];
                patient.PatientUserInfo_State = (String)dr["PatientUserInfo_State"];
                list.Add(patient);
            }
            return list;
        }

        public int GetPatientUserInfoCount(string where)
        {
            int count = 0;
            StringBuilder str = new StringBuilder();
            str.Append(" select count(*) from ");
            str.Append(tableName);
            if (where != null)
            {
                str.Append(" where ");
                str.Append(where);
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            else
            {
                count = (int)DbHelperSQL.GetSingle(str.ToString());
            }
            return count;
        }
    }
}
